package cn.wellwan.observer;

import java.util.ArrayList;

/**
 * creator: maokai
 * on: 2018/3/27
 * description: 气象站
 */
public class WeatherData implements Subject {

    private ArrayList<Observer> observerList;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData () {
        observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver (Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver (Observer observer) {
        int i = observerList.indexOf(observer);
        if (i > 0) {
            observerList.remove(i);
        }
    }

    @Override
    public void modifyObservers () {
        observerList.forEach((Observer observer) -> observer.update(temperature , humidity , pressure));
    }

    /**
     * 气象站得到数据
     * @param temperature
     * @param humidity
     * @param pressure
     */
    public void setMeasurements (float temperature , float humidity , float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    /**
     * 当气象站得到更新观测值时通知观察者
     */
    public void measurementsChanged() {
        this.modifyObservers();
    }
}
